package com.example.androidproject.chat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.androidproject.chat.pojo.ChatGroupInfo;
import com.example.androidproject.databinding.FragmentChatBinding;
import com.example.androidproject.sqlite.GroupRepository;

import java.util.ArrayList;
import java.util.List;

//聊天
public class ChatFragment extends Fragment {
    private FragmentChatBinding binding;
    private ChatAdapter adapter;
    private List<GroupRepository.GroupRecord> allGroups;
    private GroupRepository groupRepository;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentChatBinding.inflate(inflater, container, false);
        groupRepository = new GroupRepository(this.getContext());
        allGroups = groupRepository.getAllGroups();

        // 创建适配器并传入点击事件处理逻辑
        adapter = new ChatAdapter(allGroups, new ChatAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(GroupRepository.GroupRecord groupRecord) {
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                intent.putExtra("CHAT_GROUP_INFO", groupRecord.getGroupName());
                startActivity(intent);
            }
        });
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        return binding.getRoot();
    }

}
